<?php

class PosterHomeController extends Controller
{
    public function init()
    {
        parent::init();
        $this->R->module = "广告管理";
    }
	public function actionIndex()
	{
        $list = PosterHome::model()->findAll('ParentID=:ParentID',array(':ParentID'=>'0'));
        foreach($list as $v){
            $arr[] = $v->attributes;
        }
		$this->render('index',array('arr'=>$arr));
	}

    public function actionList()
    {
        $arr = array();
        $criteria = new CDbCriteria();
        $criteria->order = '`ParentID`,`SortOrder` asc';
        $count = PosterHome::model()->count($criteria);
        $arr['total'] = $count;
        $models = PosterHome::model()->findAll($criteria);
        foreach ($models as $m)
        {
            $arr['rows'][] = $m->attributes;
        }
        $arr['rows'] = $this->tree($arr['rows']);
        $arr['rows'] = array_values($arr['rows']);
        echo json_encode($arr);exit;
       }

    public function actionPosterAdd()
    {
//        $model=new UploadFile('image');
//        var_dump($model);die;
//        $image=CUploadedFile::getInstance($model,'image');
//        var_dump($image);die;
//        $model->product_imgage = $imgage;
//        $model->BookImg='/upload/123.jpg';
////        {
////            $model->BookImg=’D:/aaa/aaa.jpg’;  //请根据自己的需求生成相应的路径，但是要记得和下面保存路径保持一致
////        }
//        $image->saveAs('/upload/123.jpg');
        $type = $this->getParam('type');
        if($type == 'poster')
        {
              $this->R->action = "添加广告位";
              $arr = array('image/gif','image/jpg','image/pjpeg','image/png','image/x-png','image/bmp');
                $Form = new PosterForm('PosterAdd');
                $Form->attributes = $_POST;
                if($Form->validate())
                {
                    $filename = time();
                    $filetype = 'gif|jpeg|png|jpg';
                    $file= new UpImg('./upload/file/',$filename,$filetype);
                    $file->upload_file($_FILES['filename']);
                    $file->dst_path = './upload/Thumbnail/';
                    $file->img_resized(50,100);
                    $path = $file->uploaded;
                    $thumbnail_path = $file->thumbnail_path;
                    $model = new PosterHome();
                    $model->Title = $this->getParam('Title');
                    $model->ParentID = $this->getParam('ParentID');
                    $model->SortOrder = $this->getParam('SortOrder');
                    $model->Show = $this->getParam('Show');
                    $model->AddTime = time();
                    $model->Path = $path;
                    $model->AdminPath = $thumbnail_path;
                    $model->Src = $this->getParam('Src');
                    $model->ClickNum = 0;
                    $model->Own = $this->getParam('Own');
                    $model->Relation = $this->getParam('Relation');
                    if($model->save()>0)
                    {
                        $this->R->success = true;
                    }
                    else
                    {
                        $this->R->success = false;
                        $this->R->msg = '失败';
                    }

                }
                else
                {
                    $this->R->success = false;
                    $this->R->msg = '验证失败';
                }
        }
        elseif($type == 'group')
        {
            $this->R->action = "添加广告组";
            $Form = new PosterForm('GroupAdd');
            $Form->attributes = $_POST;
            if($Form->validate())
            {
                    $model = new PosterHome();
                    $model->Title = $this->getParam('Title');
                    $model->ParentID = 0;
                    $model->SortOrder = $this->getParam('SortOrder');
                    $model->Show = $this->getParam('Show');
                    $model->AddTime = time();
                    if($model->save()>0){
                         $this->R->success = true;
                    }
                    else
                    {
                         $this->R->success = true;
                         $this->R->msg = '添加失败';
                    }
            }
        }
        $this->R->detail = 'ID';
        $this->R->flush();

    }

    public function actionPosterEdit()
    {
        $type = $this->getParam('type');
        $id = $this->getParam('ID');
        if($type == 'poster')
        {
            $this->R->action = "修改广告位";
            $Form = new PosterForm('PosterAdd');
            $Form->attributes = $_POST;
            if($Form->validate())
            {
                $model = PosterHome::model()->findByPk($id);
                $model->Title = $this->getParam('Title');
                $model->ParentID = $this->getParam('ParentID');
                $model->SortOrder = $this->getParam('SortOrder');
                $model->Show = $this->getParam('Show');
                $model->Src = $this->getParam('Src');
                $model->Own = $this->getParam('Own');
                $model->Relation = $this->getParam('Relation');
                $model->ID = $id;
                if($model->save())
                {
                    $this->R->success = true;
                }
                else
                {
                    $this->R->success = false;
                    $this->R->msg = '失败';
                }

            }
        }
        elseif($type == 'group')
        {
            $this->R->action = "修改广告组";
            $Form = new PosterForm('GroupAdd');
            $Form->attributes = $_POST;
            if($Form->validate())
            {
                    $model = PosterHome::model()->findByPk($id);
                    $model->Title = $this->getParam('Title');
                    $model->SortOrder = $this->getParam('SortOrder');
                    if($model->save()){
                         $this->R->success = true;
                    }
                    else
                    {
                         $this->R->success = true;
                         $this->R->msg = '添加失败';
                    }
            }
        }
        $this->R->detail = 'ID';
        $this->R->flush();
    }

    public function actionPosterDel()
    {
        if($id = $this->getParam('id'))
        {
            $ParentID = $this->getParam('ParentID');
            $this->R->action='删除广告';
            if($ParentID == 0)
            {
                $result = PosterHome::model()->deleteAll("ID=:ID or ParentID=:ParentID",array(":ID"=>$id,":ParentID"=>$id));
            }
            else
            {
                $result = PosterHome::model()->deleteByPk($id,"ID=:ID",array(":ID"=>$id));
            }
            if($result)
            {
                $this->R->success = true;
            }
            else
            {
                $this->R->success = false;
                $this->R->msg = '删除失败';
            }
            $this->R->detail = 'ID';
            $this->R->flush();
        }
    }

    public function tree ($list)
     {
      foreach($list as $k=>$v ){
        if($v['ParentID'] == 0 ){
            $v['iconCls'] = 'icon-ok';
            $v['state'] = 'closed';
            $v['code'] = sprintf('%02d',$k+1);
            $arr[$v['ID']] = $v;
        }else{
            $arr[$v['ParentID']]['number']++;
            $v['code'] = $arr[$v['ParentID']]['code'].sprintf('%02d',$arr[$v['ParentID']]['number']);
            $arr[$v['ParentID']]['children'][]=$v;
        }
      }
      return $arr;
     }


	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}